package com.example.administrator.matriximage.viewpack;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.media.Image;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

import com.example.administrator.matriximage.viewpack.model.DotRecord;
import com.example.administrator.matriximage.viewpack.model.LineRecord;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PaintView extends ImageView implements IEventPost{

    public PaintView(Context context) {
        super(context);
        initPaint();
    }

    Paint drawPaint=null;
    private void initPaint() {
        drawPaint=new Paint();
        drawPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        drawPaint.setColor(Color.RED);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeWidth(10);

    }

    public PaintView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public PaintView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    Stack<LineRecord> stack=new Stack<>();
    LineRecord lineRecord=null;


    Path dstPath=null;
    Matrix invertMatrix=null;
    List<DotRecord> dotRecords=null;
    @Override
    public void downEvent(MotionEvent event) {
        invertMatrix=new Matrix();
        matrix.invert(invertMatrix);



        float[] srcPoint=new float[2];
        srcPoint[0]=event.getX();
        srcPoint[1]=event.getY();
        float[] dstPoint=new float[2];
        invertMatrix.mapPoints(dstPoint,srcPoint);
        dstPath=new Path();
        dstPath.moveTo(dstPoint[0],dstPoint[1]);
        dstCanvas.drawPath(dstPath,drawPaint);
        invalidate();

        dotRecords=new ArrayList<>();
        lineRecord=new LineRecord();
        lineRecord.setColor(Color.RED);
        lineRecord.setDotRecordList(dotRecords);

        addLine(dstPoint,event.getAction());

    }

    void addLine(float[] dstPoint,int action)
    {

        DotRecord dotRecord=new DotRecord();
        dotRecord.setAction(action);
        dotRecord.setX(dstPoint[0]);
        dotRecord.setY(dstPoint[1]);
        dotRecords.add(dotRecord);
    }


    @Override
    public void moveEvent(MotionEvent event) {


        float[] srcPoint=new float[2];
        srcPoint[0]=event.getX();
        srcPoint[1]=event.getY();
        float[] dstPoint=new float[2];
        invertMatrix.mapPoints(dstPoint,srcPoint);

        dstPath.lineTo(dstPoint[0],dstPoint[1]);
        dstCanvas.drawPath(dstPath,drawPaint);
        invalidate();

        addLine(dstPoint,event.getAction());

    }


    @Override
    public void upEvent(MotionEvent event) {


        float[] srcPoint=new float[2];
        srcPoint[0]=event.getX();
        srcPoint[1]=event.getY();
        float[] dstPoint=new float[2];
        invertMatrix.mapPoints(dstPoint,srcPoint);

        dstPath.lineTo(dstPoint[0],dstPoint[1]);
        dstCanvas.drawPath(dstPath,drawPaint);

        invalidate();

        addLine(dstPoint,event.getAction());
        stack.push(lineRecord);
    }





    @Override
    public void scaleBeginEvent(ScaleGestureDetector detector) {

    }

    @Override
    public void scaleEvent(ScaleGestureDetector detector) {

    }

    @Override
    public void scaleEndEvent(ScaleGestureDetector detector) {

    }


    @Override
    public void revoke() {

        dstCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        stack.pop();
        Paint  recordPaint=new Paint();
        recordPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        recordPaint.setColor(Color.RED);
        recordPaint.setStyle(Paint.Style.STROKE);
        recordPaint.setStrokeWidth(10);

        for(LineRecord lineRecord:stack)
        {
            recordPaint.setColor(lineRecord.getColor());
            Path  recordPath=null;
            for(DotRecord dotRecord:lineRecord.getDotRecordList())
            {
                switch (dotRecord.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {
                        recordPath=new Path();
                        recordPath.moveTo(dotRecord.getX(),dotRecord.getY());
                    }
                    break;
                    case MotionEvent.ACTION_MOVE:
                    {
                        recordPath.lineTo(dotRecord.getX(),dotRecord.getY());
                    }
                    break;
                    case MotionEvent.ACTION_UP:
                    {
                        recordPath.lineTo(dotRecord.getX(),dotRecord.getY());
                    }
                    break;

                }
                dstCanvas.drawPath(recordPath,recordPaint);

            }

        }

        invalidate();


    }



    private Matrix matrix;

    public void updateMatrix(Matrix matrix) {
          this.matrix=matrix;
          invalidate();
    }

    Bitmap dstImage=null;
    Canvas dstCanvas=null;

    final String tag=getClass().getName();
    public void initBitmap(Rect rect) {

        android.util.Log.e(tag,rect.toShortString());
        dstImage=Bitmap.createBitmap(rect.width(),rect.height(), Bitmap.Config.ARGB_8888);
        dstCanvas=new Canvas(dstImage);
        invalidate();
    }


    @Override
    protected void onDetachedFromWindow() {

        if(dstImage!=null&&!dstImage.isRecycled())
        {
            dstImage.recycle();
            dstImage=null;
        }

        super.onDetachedFromWindow();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


         if(dstImage!=null&&matrix!=null)
         {
            canvas.drawBitmap(dstImage,matrix,null);
         }







    }



}
